import { Injector } from "../../base/utils/Injector";
import { IADAble } from "./IADAble";
import { AnalyticsAble } from "./IAnalyticsAble";
import { IDevice } from "./IDevice";
import { ILoginAble } from "./ILoginAble";
import { IPayAble } from "./IPayAble";
import { IPublisher } from "./IPublisher";
import { IPublisherManager } from "./IPublisherManager";
import { IRecordAble } from "./IRecordAble";
import { IShareAble } from "./IShareAble";

/*
@name: PublisherManager
@desc: 多平台管理，集成平台的登录，支付等功能。
@author: timoo
@date: 2022/11/26
*/
export class PublisherManager {
    /**
     * 全局唯一注入KEY
     */
    static KEY: string = "PublisherManager";
    private static created:boolean = false;
    constructor(){
        if(PublisherManager.created){
            throw new Error("PublisherManager 是单例");
        }
        PublisherManager.created = true;
    }
    
    public get publisher():IPublisher{
        return this.impl.publisher;
    }
    
    /**
     * 带泛型的publisher
     * @returns 
     */
    public publisherT<T extends IPublisher>():T {
        return <T>this.publisher;
    }

    public get type():string{
        return this.impl.type;
    }

    public get appId():string{
        return this.impl.appId;
    }

    public get appVersion():string{
        return this.impl.appVersion;
    }

    public isDebug():boolean{
        return this.impl.isDebug();
    }

    /**
     * 是否是目标平台
     * @param type 目标平台
     * @returns 
     */
    public isTarget(type:string):boolean{
        return this.impl.isTarget(type);
    }

    /**
     * 广告能力banner、插屏、激励视频等
     */
    public get ad():IADAble{
        return this.impl.ad;
    }

    /**
     * 带泛型的ad
     * @returns 
     */
    public adT<T extends IADAble>():T {
        return <T> this.ad;
    }

    /**
     * 埋点统计能力
     */
    public get analytics():AnalyticsAble{
        return this.impl.analytics;
    }

    /**
     * 带泛型的analytics
     * @returns 
     */
    public analyticsT<T extends AnalyticsAble>():T {
        return <T>this.analytics;
    }

    /**
     * 平台登录能力
     */
    public get login():ILoginAble{
        return this.impl.login;
    }

    /**
     * 带泛型的login
     * @returns 
     */
    public loginT<T extends ILoginAble>():T {
        return <T> this.login;
    }

    /**
     * 支付能力
     */
    public get pay():IPayAble{
        return this.impl.pay;
    }

    /**
     * 带泛型的pay
     * @returns 
     */
    public payT<T extends IPayAble>():T {
        return <T> this.pay;
    }

    /**
     * 录屏能力
     */
    public get record():IRecordAble{
        return this.impl.record;
    }

    /**
     * 带泛型的record
     * @returns 
     */
    public recordT<T extends IRecordAble>():T {
        return <T> this.record;
    }

    /**
     * 分享能力，微信分享、抖音分享等
     */
    public get share():IShareAble{
        return this.impl.share;
    }

    /**
     * 带泛型的share
     * @returns 
     */
    public shareT<T extends IShareAble>():T {
        return <T> this.share;
    }

    /**
     * 设备相关操作[震动、电量、网络状况等]
     */
    public get device():IDevice{
        return this.impl.device;
    }

    /**
     * 带泛型的device
     * @returns 
     */
    public deviceT<T extends IDevice>():T {
        return <T> this.device;
    }

    public enable():void{
        this.impl.enable();
        // this.analytics && this.analytics.report("launch", {});
    }

    private _impl: IPublisherManager;
    private get impl(): IPublisherManager {
        if (this._impl == null) {
            this._impl = Injector.getInject(PublisherManager.KEY);
        }
        if (this._impl == null) {
            throw new Error(PublisherManager.KEY + "未注入！");
        }
        return this._impl;
    }
}
